package com.focusai.efairy.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.focusai.efairy.EFApplication;
import com.focusai.efairy.R;


/**
 * 子元素的EditText设置android:background="@null"，已设置左右padding=12dp
 * Created by cwf on 15/7/7.
 */
public class LineLinearLayout extends LinearLayout implements
        View.OnFocusChangeListener {

    private Paint mPaint;
    public static final int STATUS_FOCUSED = 1;
    public static final int STATUS_UNFOCUSED = 2;
    private int status = 2;

    private int normalColor, focusColor, color;
    private int focusedLeftDrawableId = -1, focusedRightDrawableId = -1;// 默认的左边icon
    private int unfocusedLeftDrawableId = -1, unfocusedRightDrawableId = -1;//获取焦点后的左边icon
    private Context mContext;

    private RectF mOval;
    private RectF mFocusOval;
    private int radius;
    private int edgeLength = EFApplication.getContext().getResources().getDimensionPixelSize(R.dimen.common_bg_frame_line);

//    int x = this.getScrollX();
//    int w = this.getMeasuredWidth();
    private int width, height;

    private ImageView mLeftIcon, mRightIcon;
    private EditText mEditText;//处理焦点的输入框

    public LineLinearLayout(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public LineLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();

    }

    public LineLinearLayout(Context context, AttributeSet attrs, int defStryle) {
        super(context, attrs);
        mContext = context;
        init();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        int count = getChildCount();
        if (count > 0) {
            View child;
            for (int i = 0; i < count; i++) {
                child = getChildAt(i);
                if (child instanceof EditText) {
                    mEditText = (EditText) child;
                } else if (child instanceof ImageView) {
                    if (mLeftIcon == null) {
                        mLeftIcon = (ImageView) child;
                    } else if (mRightIcon == null) {
                        mRightIcon = (ImageView) child;
                    }
                }
            }
        }
        addListeners();
        setPadding(mContext.getResources().getDimensionPixelSize(R.dimen.common_margin_dz1), 0, mContext.getResources().getDimensionPixelSize(R.dimen.common_margin_dz1), 0);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
        radius = Math.round(height / 4);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
//        mPaint.setStrokeWidth(2.0f);
        normalColor = mContext.getResources().getColor(R.color.edittext_bg_color);
        focusColor = mContext.getResources().getColor(R.color.btn_normal);
        color = normalColor;
        setStatus(status);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        int count = canvas.save();
        mPaint.setColor(color);
        if (mOval == null) {
            mOval = new RectF(0, 0, width, height);
        }
        canvas.drawRoundRect(mOval, radius, radius, mPaint);
        if (status == STATUS_FOCUSED) {
            if (mFocusOval == null) {
                mFocusOval = new RectF(edgeLength, edgeLength, width - edgeLength, height - edgeLength);
            }
            mPaint.setColor(normalColor);
            mPaint.setStyle(Paint.Style.FILL);
            canvas.drawRoundRect(mFocusOval, radius, radius, mPaint);
        }
        canvas.restoreToCount(count);

        super.dispatchDraw(canvas);
//        mPaint.setColor(color);
//        x = this.getScrollX();
//        w = this.getMeasuredWidth();
//        canvas.drawLine(0, this.getHeight() - 1, w + x,
//                this.getHeight() - 1, mPaint);//底部线
    }

    public void setStatus(int status) {
        this.status = status;
        if (status == STATUS_FOCUSED) {
            if (mLeftIcon != null && focusedLeftDrawableId != -1) {
                mLeftIcon.setImageResource(focusedLeftDrawableId);
            }
            if (mRightIcon != null && focusedRightDrawableId != -1) {
                mRightIcon.setImageResource(focusedRightDrawableId);
            }
            mPaint.setStyle(Paint.Style.FILL);
            this.color = focusColor;
        } else {
            if (mLeftIcon != null && unfocusedLeftDrawableId != -1) {
                mLeftIcon.setImageResource(unfocusedLeftDrawableId);
            }
            if (mRightIcon != null && unfocusedRightDrawableId != -1) {
                mRightIcon.setImageResource(unfocusedRightDrawableId);
            }
            mPaint.setStyle(Paint.Style.FILL);
            this.color = normalColor;
        }
        invalidate();
    }

    public void setLeftDrawable(int focusedDrawableId, int unfocusedDrawableId) {
        this.focusedLeftDrawableId = focusedDrawableId;
        this.unfocusedLeftDrawableId = unfocusedDrawableId;
        setStatus(status);
    }

    public void setRightDrawable(int focusedDrawableId, int unfocusedDrawableId) {
        this.focusedRightDrawableId = focusedDrawableId;
        this.unfocusedRightDrawableId = unfocusedDrawableId;
        setStatus(status);
    }

    private void addListeners() {
        try {
            if (mEditText != null) {
                mEditText.setOnFocusChangeListener(this);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction,
                                  Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        if (focused) {
            setStatus(STATUS_FOCUSED);
        } else {
            setStatus(STATUS_UNFOCUSED);
        }
    }

    @Override
    public void onFocusChange(View arg0, boolean arg1) {
        // TODO Auto-generated method stub
        if (arg1) {
            setStatus(STATUS_FOCUSED);
        } else {
            setStatus(STATUS_UNFOCUSED);
        }
    }
}